﻿using System;

namespace CarEvaluationANN
{
    /// <summary>
    /// Includes static methods to convert types to each other
    /// </summary>
    public class UtilityConvert
    {
        /// <summary>
        /// Converts any object to bool
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <returns>bool</returns>
        public static bool GetBoolean(object obj)
        {
            return GetBoolean(obj, false);
        }

        /// <summary>
        /// Converts any object to bool
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <param name="defaultValue">default value to return</param>
        /// <returns>bool</returns>
        public static bool GetBoolean(object obj, bool defaultValue)
        {
            var a = defaultValue;
            if (obj is string)
            {
                if ((string) obj == "1")
                    return true;
                else if ((string) obj == "0")
                    return false;
                else
                {
                    var isParsed = Boolean.TryParse((string) obj, out a);
                    if (!isParsed)
                        return defaultValue;
                }
            }
            else
            {
                try
                {
                    a = Convert.ToBoolean(obj);
                }
                catch
                {
                }
            }
            return a;
        }

        /// <summary>
        /// Converts any object to byte
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <returns>byte</returns>
        public static byte GetByte(object obj)
        {
            return GetByte(obj, 0);
        }

        /// <summary>
        /// Converts any object to byte
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <param name="defaultValue">default value to return</param>
        /// <returns>byte</returns>
        public static byte GetByte(object obj, byte defaultValue)
        {
            byte a = defaultValue;
            if (obj is string)
            {
                bool isParsed = Byte.TryParse((string) obj, out a);
                if (!isParsed)
                    return defaultValue;
            }
            else
            {
                try
                {
                    a = Convert.ToByte(obj);
                }
                catch
                {
                }
            }
            return a;
        }

        /// <summary>
        /// Converts any object to short
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <returns>short</returns>
        public static short GetInt16(object obj)
        {
            return GetInt16(obj, 0);
        }

        /// <summary>
        /// Converts any object to short
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <param name="defaultValue">default value to return</param>
        /// <returns>short</returns>
        public static short GetInt16(object obj, short defaultValue)
        {
            short a = defaultValue;
            if (obj is string)
            {
                bool isParsed = Int16.TryParse((string) obj, out a);
                if (!isParsed)
                    return defaultValue;
            }
            else
            {
                try
                {
                    a = Convert.ToInt16(obj);
                }
                catch
                {
                }
            }
            return a;
        }

        /// <summary>
        /// Converts any object to int
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <returns>int</returns>
        public static int GetInt32(object obj)
        {
            return GetInt32(obj, 0);
        }

        /// <summary>
        /// Converts any object to int
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <param name="defaultValue">default value to return</param>
        /// <returns>int</returns>
        public static int GetInt32(object obj, int defaultValue)
        {
            int a = defaultValue;
            if (obj is string)
            {
                bool isParsed = Int32.TryParse((string) obj, out a);
                if (!isParsed)
                    return defaultValue;
            }
            else
            {
                try
                {
                    a = Convert.ToInt32(obj);
                }
                catch
                {
                }
            }
            return a;
        }

        /// <summary>
        /// Converts any object to float
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <returns>float</returns>
        public static float GetSingle(object obj)
        {
            return GetSingle(obj, 0);
        }

        /// <summary>
        /// Converts any object to float
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <param name="defaultValue">default value to return</param>
        /// <returns>float</returns>
        public static float GetSingle(object obj, float defaultValue)
        {
            float a = defaultValue;
            if (obj is string)
            {
                bool isParsed = Single.TryParse((string) obj, out a);
                if (!isParsed)
                    return defaultValue;
            }
            else
            {
                try
                {
                    a = Convert.ToSingle(obj);
                }
                catch
                {
                }
            }
            return a;
        }

        /// <summary>
        /// Converts any object to long
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <returns>long</returns>
        public static long GetInt64(object obj)
        {
            return GetInt64(obj, 0);
        }

        /// <summary>
        /// Converts any object to long
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <param name="defaultValue">default value to return</param>
        /// <returns>long</returns>
        public static long GetInt64(object obj, long defaultValue)
        {
            long a = defaultValue;
            if (obj is string)
            {
                bool isParsed = Int64.TryParse((string) obj, out a);
                if (!isParsed)
                    return defaultValue;
            }
            else
            {
                try
                {
                    a = Convert.ToInt64(obj);
                }
                catch
                {
                }
            }
            return a;
        }

        /// <summary>
        /// Converts any object to double
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <returns>double</returns>
        public static double GetDouble(object obj)
        {
            return GetDouble(obj, 0);
        }

        /// <summary>
        /// Converts any object to double
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <param name="defaultValue">default value to return</param>
        /// <returns>double</returns>
        public static double GetDouble(object obj, double defaultValue)
        {
            double a = defaultValue;
            if (obj is string)
            {
                bool isParsed = Double.TryParse((string) obj, out a);
                if (!isParsed)
                    return defaultValue;
            }
            else
            {
                try
                {
                    a = Convert.ToDouble(obj);
                }
                catch
                {
                }
            }
            return a;
        }

        /// <summary>
        /// Converts any object to decimal
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <returns>decimal</returns>
        public static decimal GetDecimal(object obj)
        {
            return GetDecimal(obj, 0);
        }

        /// <summary>
        /// Converts any object to decimal
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <param name="defaultValue">default value to return</param>
        /// <returns>decimal</returns>
        public static decimal GetDecimal(object obj, decimal defaultValue)
        {
            decimal a = defaultValue;
            if (obj is string)
            {
                bool isParsed = Decimal.TryParse((string) obj, out a);
                if (!isParsed)
                    return defaultValue;
            }
            else
            {
                try
                {
                    a = Convert.ToDecimal(obj);
                }
                catch
                {
                }
            }
            return a;
        }

        /// <summary>
        /// Converts any object to DateTime
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <returns>DateTime</returns>
        public static DateTime GetDateTime(object obj)
        {
            return GetDateTime(obj, DateTime.MinValue);
        }

        /// <summary>
        /// Converts any object to DateTime
        /// </summary>
        /// <param name="obj">object to convert</param>
        /// <param name="defaultValue">default value to return</param>
        /// <returns>DateTime</returns>
        public static DateTime GetDateTime(object obj, DateTime defaultValue)
        {
            DateTime a = defaultValue;
            if (obj is string)
            {
                bool isParsed = DateTime.TryParse((string) obj, out a);
                if (!isParsed)
                    return defaultValue;
            }
            else
            {
                try
                {
                    a = Convert.ToDateTime(obj);
                }
                catch
                {
                }
            }
            return a;
        }
    }
}